/*
 * @lc app=leetcode.cn id=203 lang=cpp
 *
 * [203] 移除链表元素
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        //设立dummy节点
        //每一次删除等于val值的一串节点
        ListNode* dummy = new ListNode(-1);
        dummy -> next = head;
        ListNode * temp = dummy;
        while(head != nullptr){
            if(head->val == val){
                while(head && head->val == val) head = head->next;
                temp ->next = head;
            }
            else{
                temp = head;
                head = head->next;
            }
        }
        return dummy->next;
    }
};
// @lc code=end

